<?php 

Class menuModel extends appModel
{
	private $option=array();
	private $loop_num=0;
	public function ordering($val,$id)
	{
		$result=array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('category')." SET ordering='$val' WHERE id='".$id."'";
		$res=$this->query($sql);
		if(!res)
		{
			$result['error']=1;
			$result['content']='';
		}
		return $result;
	}
    public function get_parent_menu($type='')
    {
    	
    	$sql="SELECT name,id,type FROM ".$this->table('category')." WHERE parent_id=0 "." ORDER BY id";
    	$arr=$this->getAll($sql);
    	foreach($arr as $k=> $v)
    	{
    		$arr[$k]['name']=hy_dig_filter($v['name'],1);
    		$arr[$k]['url']='index.php?g='.GROUP_NAME.'&m='.MODULE_NAME.'&a='.ACTION_NAME.'&cat_id='.$v['id'];
    	}
    	$result=$this->get_child($arr,1,0,$type);
    	foreach($result as $k=>$v)
    	{
    		if(is_array($type))
    		{
    			if(!empty($type)&&!in_array($v['type'],$type))
    			{
    				unset($result[$k]);
    			}
    		}	
    		else 
    		{
    		if($type!=''&&$v['type']!=$type)
    		{
    			unset($result[$k]);
    		}
    		}
    	}	
    	array_unshift($result,array('name'=>'--- --- ---','id'=>0));
    	
    	return $result; 
    }
    /**
     * 获取菜单的子菜单列表
     *
     * @access      public
     * @param       array      arr     当前级别菜单数组
     * @param       int      num     当前级别
     * @param       int     show_extend_link     是否显示外链
     * @return      array
     */
   private function get_child($arr,$show_extend_link=1,$num=0,$type='')
   {
   	
   	if($show_extend_link==0)
   	{
   		$where.=' AND is_extend_links = 0';
   	}
   	if($type!='')
   	{
   		//$where.=" AND type='".$type."'";
   	}
  
   	   foreach($arr as $k=>$v)
     {
   		  $sql="SELECT id,name,type,ordering FROM ".$this->table('category')." WHERE parent_id=".$v['id'].$where." ORDER BY id";
   		  $temp=$this->getAll($sql);
   		  
   		 $left_char=str_repeat('----|', $num);
   		  $this->option[$v['id']]['name']=$left_char.=hy_dig_filter($v['name'],1);
   		  $this->option[$v['id']]['url']='index.php?g='.GROUP_NAME.'&m='.MODULE_NAME.'&a='.ACTION_NAME.'&cat_id='.$v['id'];
   		  $this->option[$v['id']]['id']=$v['id'];
   		  $this->option[$v['id']]['type']=$v['type'];
   		  switch ($v['type'])
   		  {
   		  	case 1: $this->option[$v['id']]['cat_type']='文章模块';break;
   		  	case 2: $this->option[$v['id']]['cat_type']='相册模块';break;
   		  	case 3: $this->option[$v['id']]['cat_type']='图文模块';break;
   		  	case 4: $this->option[$v['id']]['cat_type']='产品模块';break;
   		  }
   		  
   		  $this->option[$v['id']]['code']=$v['code'];
   		  $this->option[$v['id']]['ordering']=$v['ordering'];
   		  if($temp)
   		 {
   		 	$this->loop_num=$num;
   		 	$this->loop_num++;
   		 	$this->get_child($temp,$show_extend_link,$this->loop_num,$type);
   		 }
   		 else 
   		 {
   		 	$this->loop_num=0;
   		 }
   	  }
   	  return $this->option;
   } 
   public function get_menu_info($id)
   {
   	$sql=" SELECT * FROM ".$this->table('category')." WHERE id=".$id;
   	$info=$this->getRow($sql);
   	$arr3=hy_dig_filter($info['name'],3);
	$info['en_name']=$arr3[1];
	$info['name']=$arr3[0];
   	$sql=" SELECT name FROM ".$this->table('category')." WHERE id=".$info['parent_id'];
   	$info['parent_name']=hy_dig_filter($this->getOne($sql),1);
   	
   	switch ($info['type'])
   	{
   		case 1:$info['type_name']='文章模块';break;
   		case 2:$info['type_name']='相册模块';break;
   		case 3:$info['type_name']='图文模块';break;
   		case 4:$info['type_name']='产品模块';break;
   	}
   	return $info;
   }
   
   public function del_menu($id,$is_all=false)
   {
   
   	$result=array('error'=>0,'content'=>'删除成功');
   	if($id)
   	{
   		$sql="SELECT count(*) FROM ".$this->table('category')." WHERE parent_id=".$id;
   		$child_menu=$this->getOne($sql);
   		
   		if($child_menu>0)
   		{
   			$result['error']=1;
   			$result['content']='栏目存在子级别栏目';
   			return $result;
   		
   		}
   		if($is_all==true)
   		{
   			$sql="SELECT type FROM ".$this->table('category')." WHERE id=".$id;
   			$type=$this->getOne($sql);

   			if(in_array($type,array(1,2,3,4)))
   			{
   				switch ($type)
   				{
   					case 1:$table=$this->table('article');break;
   					case 2:$table=$this->table('albums');break;
   					case 3:$table=$this->table('single_pages');break;
   					case 4:$table=$this->table('product');break;
   				}
   				$sql="SELECT count(*) FROM ".$table." WHERE cat_id='".$id."'";
   			
   				$content_num=$this->getOne($sql);
   				if($content_num>0&&$type!=3)
   				{
   					$result['error']=1;
   					$result['content']='栏目下面存在内容';
   					return $result;
   			
   				}
   				else
   				{
   					$sql="DELETE FROM ".$this->table('category')." WHERE  id='".$id."'";
   			
   					$res=$this->query($sql);
   					if($type==3)
   					{
   						$sql="DELETE FROM ".$this->table('single_pages')." WHERE  cat_id='".$id."'";
   			
   						$res=$this->query($sql);
   					}
   					if(!$res)
   					{
   						$result['error']=1;
   						$result['content']='删除失败';
   						return $result;
   					}
   				}
   			}
   		}

   		else
   		{
   			$sql="DELETE FROM ".$this->table('category')." WHERE  id='".$id."'";
   			
   			$res=$this->query($sql);
   			 
   			if(!$res)
   			{
   				$result['error']=1;
   				$result['content']='删除失败';
   				return $result;
   			}
   		}
   	    
   	   
   		
   			
   			
   	}
   	return $result;
   }
   /**
    * 不分页获取菜单列表
    *
    * @access      public
    * @param       int      page     当前页
    * @param       int      pagesize     每页显示数量
    * @param       int     show_extend_link     是否显示外链
    * @return      array
    */
   public function get_menu_list($page=1,$page_size=10,$show_extend_link=1)
   {
   	$listPage=array('data'=>'','pageLink'=>'');
   	$new_menu=array();
   	$where='';
   	if($show_extend_link==0)
   	{
   		$where.=' AND is_extend_links = 0';
   	}
   	$sql="SELECT id,name,ordering FROM ".$this->table('category')." WHERE parent_id=0".$where;
   	$arr=$this->getAll($sql);
   	$all_menu=$this->get_child($arr,$show_extend_link);//全部菜单
   	 
   	$record_nums=count($all_menu);
   	$page_count = ceil($record_nums/$page_size);
   	 
   	$select_limit = $page_size;
   	$select_from = ($page - 1) * $page_size;
   	$pre_page = ($page == 1)? 1 : $page - 1;
   	$next_page= ($page == $page_count)? $page_count : $page + 1 ;
   	 
   	$i=0;
   	$j=1;
   	 
   	foreach($all_menu as $k=>$v)
   	{
        
	    $all_menu [$k]['name']=hy_dig_filter($v['name'],1);
	
   		if($i>=$select_from)
   		{
   			 
   			 
   			$new_menu[]=$all_menu[$k];
   			 
   			if($j>=$select_limit)
   			{
   				//break;
   			}
   			$j++;
   		}
   
   		$i++;
   	}
   		$page=new Page($page,$page_count);
   		$page->create_arr();
   		
   	$listPage['pagelink']=$page->html_page();
   	$listPage['data']=$new_menu;
   	return $listPage;
   }
   /**
    * 分页获取菜单列表
    *
    * @access      public
    * @param       int      page     当前页
    * @param       int      pagesize     每页显示数量
    * @param       int     show_extend_link     是否显示外链
    * @return      array
    */
    public function get_menu_list2($page=1,$page_size=10,$sort)
    {
    	$listPage=array('data'=>'','pageLink'=>'');
    	$new_menu=array();
    	$where='';
        if($sort['keyword']!='')
		{
			$where.=" AND name like '%".$sort['keyword']."%'";
		}
    	$sql="SELECT * FROM ".$this->table('category')." WHERE parent_id=0".$where." ORDER BY id DESC";
    	$arr=$this->getAll($sql);
    	
    	$all_menu=$this->get_child($arr,$show_extend_link);//全部菜单
    	
    	$record_nums=count($all_menu);
    	$page_count = ceil($record_nums/$page_size);
    	
    	$select_limit = $page_size;
    	$select_from = ($page - 1) * $page_size;
    	$pre_page = ($page == 1)? 1 : $page - 1;
    	$next_page= ($page == $page_count)? $page_count : $page + 1 ;
    	
    	$i=0;
    	$j=1;
    	
    	foreach($all_menu as $k=>$v)
    	{
    		
	        $all_menu [$k]['name']=hy_dig_filter($v['name'],1);
	       
    		if($i>=$select_from)
    		{
    			
    			
    			 $new_menu[]=$all_menu[$k];
    			
    			 if($j>=$select_limit)
    			 {
    			 	break;
    			 }
    			 $j++;
    		}
    		
    		$i++;
    	}
    	$page_obj=new Page($page,$record_nums);
    	$page_obj->create_arr();
   	    $listPage['pagelink']=$page_obj->html_page();
    	$listPage['data']=$new_menu;
    	return $listPage;
    }
    public function insert_menu($info)
	{
		$field='';
		$value='';
		$result=array('error'=>0,'content'=>'');
		
		
		
			
			foreach($info as $k=>$v)
			{
				if(!$v==='')
				{
					
			
				}
				else 
				{
					$field.="$k,";
					
					
					$value.="'$v',";
				}
			}
			
			$field.="created";
			$value.="'".$_SERVER['REQUEST_TIME']."'";
			
			$sql="INSERT INTO ".$this->table('category')." (".$field.") VALUES(".$value.")";
			
			if(!$res=$this->query($sql))
			{
				$result['error']=1;
				$result['content']=Lang('SQL_ERROR');
			}
		
		return $result;
		
	}
	public function update_menu($info,$id)
	{
		if($id)
		{
			
			foreach($info as $k=>$v)
			{
				
					$where.=" $k='$v',";
	
				
			}
			$where=substr($where,0,strlen($where)-1);
	
			$sql="UPDATE ".$this->table('category')." SET ".$where." WHERE id='".$id."'";
				
			return $res=$this->query($sql);
		}
	}
	
	public function check_code($code,$id=0)
	{
		if($id>0)
		{
			$sql="SELECT code FROM ".$this->table('category')." WHERE id='".$id."'";
			$oldcode=$this->getOne($sql);
			$code_exist=0;
			if($oldcode!=$code)
			{
				$sql="SELECT * FROM ".$this->table('category')." WHERE code='$code'";
				$code_exist=$this->getOne($sql);
					
			}
				
			if($code_exist>0)
			{
	
				return false;
			}
			else
			{
				return true;
			}
		}
		else
		{
			$sql="SELECT count(*) FROM ".$this->table('category')." WHERE code='$code'";
			
			if($this->getOne($sql)>0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}